<!DOCTYPE html>
<html lang="zh-cn">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>编辑</title>
  <!-- 导入jquery -->
  <script src="./libs/jquery-1.12.4.min.js"></script>
  <!-- 导入bootstrap -->
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
  <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
  <!-- 导入art-template -->
  <script src="./libs/template-web.js"></script>
  <script src="./libs/mod.js"></script>
  <style>
    .btn-exit {
      margin-top: 8px;
    }
  </style>
</head>

<body>
  <!-- bootstrap的容器  带版心的-->
  <div class="container">
    <!-- 导航 -->
    <nav class="navbar navbar-default">
      <div class=".container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <a class="navbar-brand" href="#">欢迎进入CQ后台管理</a>
        </div>
        <!-- /.navbar-collapse -->
      </div>
      <!-- /.container-fluid -->
    </nav>
    <!-- 底部的内容 -->
    <div class="row">
      <!-- 编辑内容 -->
      <div class="col-xs-9">
        <div class="panel panel-default">
          <!-- 标题 -->
          <div class="panel-heading">
            <h3 class="panel-title">
              <!-- 面包屑导航 -->
              <ol class="breadcrumb">
                <li><a href="#">首页</a></li>
                <li><a href="#">英雄管理</a></li>
                <li class="active">英雄列表</li>
              </ol>
            </h3>
          </div>

          <!-- 内容 -->
          <div class="panel-body">
            <form class="form-horizontal ajax-form" id="form">
              <!-- 隐藏域 -->
              <input type="hidden" id="heroId" name="id" value="1"/>
              <div class="form-group">
                <label for="name" class="col-sm-2 control-label">姓名</label>
                <div class="col-sm-10">
                  <input type="text" name="heroname" class="form-control" id="name" placeholder="姓名" />
                </div>
              </div>
              <div class="form-group">
                <label for="skill" class="col-sm-2 control-label">技能</label>
                <div class="col-sm-10">
                  <input type="text" name="heroskill" class="form-control" id="skill" placeholder="技能" />
                </div>
              </div>
              <div class="form-group">
                <label for="icon" class="col-sm-2 control-label">头像</label>
                <div class="col-sm-10">
                  <input type="file" name="heroicon" class="form-control" id="icon" placeholder="技能" />
                  <img class="pre-img" src="" id="iconImg" alt="" width="72px" height="72px" />
                </div>
              </div>
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <button type="submit" class="btn btn-success btn-save">
                    保存
                  </button>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>

  <script></script>
</body>

</html>
<script>
  $(function () {
    let heroId = window.location.search.split('=')[1]
    $.ajax({
      url: 'http://127.0.0.1:4099/getHeroById',
      type: 'get',
      data: {
        id: heroId
      },
      success: function (backData) {
        console.log(backData)
        if (backData.code == 200) {
          let data = backData.data[0]
          $('#name').val(data.heroname)
          $('#skill').val(data.heroskill)
          $('.pre-img').attr('src', data.heroicon)
          $('#heroid').val(heroId)
        }
      }
    })
    $('.btn-save').on('click', function (e) {
      e.preventDefault()
      let fd = new FormData(document.querySelector('form'))

      $.ajax({
        url: 'http://127.0.0.1:4099/editHero',
        data: fd,
        contentType: false,
        processData: false,
        type: 'post',
        success: function (backData) {
          if (backData.code == 200) {}
        }
      })
    })
  })
</script>